package com.greenantmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.adapter.GoodsListAdapterForImageItem;
import com.greenantmall.bean.ShoppingCart;
import com.greenantmall.bean.UIGoods;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class SearchActivity2 extends BaseActivity {
	public static final String KeySearchContent = "key_search_content";
	
	private Button mBtnBack;
	private Button mBtnClear;
	private EditText mEtSearch;
	private TextView mTvSearch;
	private TextView mTvPrompt;
	private ProgressBar mProgressBar;
	private GridView mGridView;
	
	private List<UIGoods> mDataList = new ArrayList<UIGoods>();
	private List<ShoppingCart> mShoppingCartList = new ArrayList<ShoppingCart>();
	private GoodsListAdapterForImageItem mAdapter;
	
	private boolean isLoadingData = false;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mBtnBack)) {
				SearchActivity2.this.finish();
			}
			else if(v.equals(mBtnClear)) {
				mEtSearch.setText("");
			}
			else if(v.equals(mTvSearch)) {
				doSearch(null);
			}
		}
	};
	
	private void doSearch(String searchContent) {
		if(isLoadingData) {
			UtilTools.showMyToast(SearchActivity2.this, 
					getResources().getString(R.string.search_activity_please_wait));
			return;
		}
		
		if(TextUtils.isEmpty(searchContent)) {
			searchContent = mEtSearch.getText().toString();
		}
		if(TextUtils.isEmpty(searchContent)) {
			UtilTools.showMyToast(SearchActivity2.this, 
					getResources().getString(R.string.search_activity_input_search_content));
			return;
		}
		
		RetrieveDataAsyn.getInstance().searchGoodsList(searchContent, -1, mRetrieveDataListener);
		isLoadingData = true;
		mTvPrompt.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.VISIBLE);
		mGridView.setVisibility(View.GONE);
		
		UtilTools.saveSearchHistory(getApplication(), searchContent);
	}
	
	private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(arg0.equals(mGridView)) {
//				UIGoods uiGoods = mDataList.get(arg2);
//				Intent intent = new Intent(SearchActivity2.this, DetailActivity.class);
//				intent.putExtra(DetailActivity.KEY_DETAIL_OBJECTID, uiGoods.getObjectId());
//				SearchActivity2.this.startActivity(intent); 
			}
		}
	};
	
	private RetrieveDataListener mRetrieveDataListener = new RetrieveDataListener() {
		@Override
		public void dataRetrieved(int requestId, Object data) {
			isLoadingData = false;
			mProgressBar.setVisibility(View.GONE);
			mDataList.clear();
			
			if(data != null) {
				List<UIGoods> uiGoodsList = (List<UIGoods>)data;
				mDataList.addAll(uiGoodsList);
			}
			
			if(mDataList.size() > 0) {
				mTvPrompt.setVisibility(View.GONE);
				mGridView.setVisibility(View.VISIBLE);
				mAdapter.notifyDataSetInvalidated();
			}
			else {
				mTvPrompt.setVisibility(View.VISIBLE);
				mGridView.setVisibility(View.GONE);
				mTvPrompt.setText(R.string.search_activity_no_search_result);
			}
		}
	};
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(Const.SHOPPING_CART_CHANGED)) {
				refreshShoppingCartList();
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.search_activity2, getResources().getColor(R.color.green));
		
		mBtnBack = (Button)findViewById(R.id.search_activity_top_arrow_left);
		mBtnClear = (Button)findViewById(R.id.search_activity_cancel_icon);
		mEtSearch = (EditText)findViewById(R.id.search_activity_et);
		mTvSearch = (TextView)findViewById(R.id.search_activity_top_right_tv);
		mTvPrompt = (TextView)findViewById(R.id.search_activity_tv_prompt);
		mProgressBar = (ProgressBar)findViewById(R.id.search_activity_prgressbar);
		mGridView = (GridView)findViewById(R.id.search_activity_gridview);
		
		mBtnBack.setOnClickListener(mOnClickListener);
		mBtnClear.setOnClickListener(mOnClickListener);
		mTvSearch.setOnClickListener(mOnClickListener);
		mGridView.setOnItemClickListener(mOnItemClickListener);
		
		mTvPrompt.setVisibility(View.VISIBLE);
		mProgressBar.setVisibility(View.GONE);
		mGridView.setVisibility(View.GONE);
		
		IntentFilter filter = new IntentFilter(Const.SHOPPING_CART_CHANGED);
        this.registerReceiver(mReceiver, filter);
		
		mAdapter = new GoodsListAdapterForImageItem(this, mDataList, mShoppingCartList);
		mGridView.setAdapter(mAdapter);
		
		String searchContent = getIntent().getStringExtra(KeySearchContent);
		if(!TextUtils.isEmpty(searchContent)) {
			mEtSearch.setText(searchContent);
			doSearch(searchContent);
		}
		
		refreshShoppingCartList();
	}
	
	@Override
    protected void onDestroy() {
    	super.onDestroy();
    	this.unregisterReceiver(mReceiver);
    }
	
	private void refreshShoppingCartList() {
		mShoppingCartList.clear();
		List<ShoppingCart> scList = UtilTools.getShoppingCartDatas();
		if(scList != null) {
			mShoppingCartList.addAll(scList);
		}
		
		if(mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}
}

